import hypermedia.net.*;
import processing.net.*;

UDP udpBroadcast;
UDP udpGui;
UDP udpDmx;
byte saxValue;

class OnOff{
  private PVector position;
  private boolean state;
  
  OnOff(PVector position){
    this.state = false ;
    this.position = position;
  }
 
 void toggle(){
   state = !state;
 }
 
 void draw(){
   fill(255,0,0);
   if(this.state)fill(0,255,0);
   ellipse(position.x, position.y, 20,20);
   
 }
  
};

ArrayList<OnOff> onOffList;
  
  


void setup() {
  size(500, 600);
  background(0);

  // Create the font
  textFont(createFont("Courrier",25));
  textAlign(LEFT);
  
  udpGui = new UDP(this, 8001, "192.168.1.16");
  udpGui.listen(false);
  
  udpDmx = new UDP(this, 8002, "192.168.1.16");
  udpDmx.listen(false);
  
  udpBroadcast = new UDP(this, 8000, "192.168.1.255");
  udpBroadcast.setBuffer(1024);
  udpBroadcast.listen(true);
  loop();
  
  onOffList = new ArrayList<OnOff>();
  onOffList.add(new OnOff(new PVector(10,30)));
  onOffList.add(new OnOff(new PVector(10,70)));
  onOffList.add(new OnOff(new PVector(10,110)));
  onOffList.add(new OnOff(new PVector(10,150)));
  onOffList.add(new OnOff(new PVector(10,190)));
  onOffList.add(new OnOff(new PVector(10,230)));
  onOffList.add(new OnOff(new PVector(10,270)));
  onOffList.add(new OnOff(new PVector(10,310)));
  onOffList.add(new OnOff(new PVector(10,350)));
  onOffList.add(new OnOff(new PVector(10,390)));
  onOffList.add(new OnOff(new PVector(10,430)));
  onOffList.add(new OnOff(new PVector(10,470)));
}

void draw() {
  background(0);
  fill(255,255,255);
  //println("toto");
  
  // Draw the letter to the screen
  text("Valeur Guillaume :" + Integer.toString(saxValue) , 20, 20);
  
  translate(0, 0);
  
  text("A:sax\nZ:rasant contre\nE:rouge interieur\nR:rouge exterieur\nT:ziodes face\nY:ACL + ziode retro\nU:sax side\nI:decoupe sax\nO:decoupe guitare\nP:decoupe basse\nQ:decoupe drum\nS:decoupe hautes", 40,40);

  //drawing all onOff
  for (OnOff onOff : onOffList){
    onOff.draw();  
  }

}

void receive(byte[] data) {
  
  for (int i=0; i < data.length; i++){
    print(data[i]);
  }
    byte saxValueB = data[0];
    int saxValue = ((int)data[0] + 127);
    println(saxValue);
    //saxValue = (data[0] & 0xff);
    //println((int)saxValue);
}

void keyReleased() {
    println("keyReleased");
    switch (key){
    case 'a' :
      udpGui.send("a","192.168.1.3");
      onOffList.get(0).toggle();
     break;
    case 'z' :
      udpDmx.send("z","192.168.1.4");
      onOffList.get(1).toggle();
     break;
    case 'e' :
      udpDmx.send("e","192.168.1.4");
      onOffList.get(2).toggle();
     break;
    case 'r' :
      udpDmx.send("r","192.168.1.4");
      onOffList.get(3).toggle();
    break;
    case 't' : 
      udpDmx.send("t","192.168.1.4");
      onOffList.get(4).toggle();
    break;
    case 'y' : 
      udpDmx.send("y","192.168.1.4");
      onOffList.get(5).toggle();
    break;
    case 'u' : 
      udpDmx.send("u","192.168.1.4");
      onOffList.get(6).toggle();
    break;
    case 'i' : 
      udpDmx.send("i","192.168.1.4");
      onOffList.get(7).toggle();
    break;
     case 'o' : 
      udpDmx.send("o","192.168.1.4");
      onOffList.get(8).toggle();
    break;
     case 'p' : 
      udpDmx.send("p","192.168.1.4");
      onOffList.get(9).toggle();
    break;
     case 'q' : 
      udpDmx.send("q","192.168.1.4");
      onOffList.get(10).toggle();
    break;
     case 's' : 
      udpDmx.send("s","192.168.1.4");
      onOffList.get(11).toggle();
    break;
    }
  
}
